[PHP] 使用Google Analytics取得網站資訊

最近幫忙做網站,說想要一個顯示瀏覽人數的工具,發現Google就有提供Google Analytics且相當的豐富,但沒想到網路上資源很少,遇到一堆問題也卡了好幾天,好不容易解決了 …

申請和連結就不贅述了,重點在於API的使用。

Google API

首先建立API的認證,這邊選擇的是用戶端的瀏覽器方法,轉址為認證完轉向的網址,通常為同一個檔案。

google-api-php-client

下載google-api-php-client到你的伺服器。

PHP

官方的參考文件,詳細的可以看一下,但有些地方沒更新。

首先是客戶端的認證,基本上除了個人設定不用做太大的變動。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
require_once 'google-api-php-client/src/Google/autoload.php';
session_start();
$client = new Google_Client();
$client->setApplicationName('name'); //名稱
$client->setClientId('id'); //client ID
$client->setClientSecret('password'); //client password
$client->setRedirectUri('URL'); //插入轉址
$client->setScopes("https://www.googleapis.com/auth/analytics.readonly");
if (isset($_GET['code'])) { //取得code的參數
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if($client->isAccessTokenExpired()) {
$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
}

再來就是使用Analytics的API去取得資訊,這裡舉取得瀏覽數為例,其它的可以參考文件,其中的id位置在Google Analytics裡的資料檢視編號

新的版本改了Class的名稱,但文件沒改過來。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (!$client->getAccessToken()) {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>"; //認證
} else {
$service = new Google_Service_Analytics($client);
$id = 'ga:xxxxx' ;
$start_date = '2010-01-01';
$end_date = 'today';
$metrics = "ga:pageviews";
$total = $service->data_ga->get($id,$start_date,$end_date,$metrics);
$rows = $total->getRows() ;
$sessions = $rows[0][0];
echo $sessions ;
}

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
require_once 'google-api-php-client/src/Google/autoload.php';
session_start();
$client = new Google_Client();
$client->setApplicationName('name'); //名稱
$client->setClientId('id'); //client ID
$client->setClientSecret('password'); //client password
$client->setRedirectUri('URL'); //插入轉址
$client->setScopes("https://www.googleapis.com/auth/analytics.readonly");
if (isset($_GET['code'])) { //取得code的參數
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if($client->isAccessTokenExpired()) {
$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
}
if (!$client->getAccessToken()) {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>"; //認證
} else {
$service = new Google_Service_Analytics($client);
$id = 'ga:xxxxx' ;
$start_date = '2010-01-01';
$end_date = 'today';
$metrics = "ga:pageviews";
$total = $service->data_ga->get($id,$start_date,$end_date,$metrics);
$rows = $total->getRows() ;
$sessions = $rows[0][0];
echo $sessions ;
}